//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    markers: [{
        id: 1,
        latitude: 30.97777,
        longitude: 104.08221,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00035",
          color: "#3b3b3b",
          fontSize: "14px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      // {
      //   id: 2,
      //   latitude: 30.9753922132,
      //   longitude: 104.0820586681,
      //   iconPath: "../images/marker.png",
      //   callout: {
      //     content: "fmxy_00036",
      //     color: "#3b3b3b",
      //     fontSize: "14px",
      //     display: "ALWAYS",
      //     bgColor: "transparent"
      //   }
      // },
      // {
      //   id: 3,
      //   latitude: 30.9762063017,
      //   longitude: 104.0826541185,
      //   iconPath: "../images/marker.png",
      //   callout: {
      //     content: "fmxy_00037",
      //     color: "#3b3b3b",
      //     fontSize: "14px",
      //     display: "ALWAYS",
      //     bgColor: "transparent"
      //   }
      // },
      {
        id: 4,
        latitude: 30.977679,
        longitude: 104.077167,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00038",
          color: "#3b3b3b",
          fontSize: "14px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      // {
      //   id: 5,
      //   latitude: 30.9748218871,
      //   longitude: 104.0828204155,
      //   iconPath: "../images/marker.png",
      //   callout: {
      //     content: "fmxy_00039",
      //     color: "#3b3b3b",
      //     fontSize: "14px",
      //     display: "ALWAYS",
      //     bgColor: "transparent"
      //   }
      // },
      {
        id: 6,
        latitude: 30.978598,
        longitude: 104.079059,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00040",
          color: "#3b3b3b",
          fontSize: "14px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      // {
      //   id: 7,
      //   latitude: 30.9753048247,
      //   longitude: 104.0855669975,
      //   iconPath: "../images/marker.png",
      //   callout: {
      //     content: "fmxy_00041",
      //     color: "#3b3b3b",
      //     fontSize: "14px",
      //     display: "ALWAYS",
      //     bgColor: "transparent"
      //   }
      // },
      {
        id: 8,
        latitude: 30.977649,
        longitude: 104.082062,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00042",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 9,
        latitude: 30.975625,
        longitude: 104.082607,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00043",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 10,
        latitude: 30.978163,
        longitude: 104.081913,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00044",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 11,
        latitude: 30.978208,
        longitude: 104.080523,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00045",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 12,
        latitude: 30.979411,
        longitude: 104.083044,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00046",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 13,
        latitude: 30.977167,
        longitude: 104.08125,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00026",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 14,
        latitude: 30.976454,
        longitude: 104.08067,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00034",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 15,
        latitude: 30.976544,
        longitude: 104.080452,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00021",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 16,
        latitude: 30.978315,
        longitude: 104.079156,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00033",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 17,
        latitude: 30.977987,
        longitude: 104.081805,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00032",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 18,
        latitude: 30.978544,
        longitude: 104.081197,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00028",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 19,
        latitude: 30.979192,
        longitude: 104.082864,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy00027",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      },
      {
        id: 20,
        latitude: 30.977015,
        longitude: 104.081709,
        iconPath: "../images/marker.png",
        callout: {
          content: "fmxy_00029",
          color: "#3b3b3b",
          fontSize: "15px",
          display: "ALWAYS",
          bgColor: "transparent"
        }
      }
    ],
    checked: '',
    list: true
  },

  onLoad: function() {
    this.map = wx.createMapContext('myMap')
    this.data.checked = wx.getStorageSync('checked')
    console.log(this.data.checked)
    if (!this.data.checked) {
      this.reset()
    }else{
      this.change_checked(this.data.checked)
    }
    this.openBluetoothAdapter()
  },

  change_page: function (event){
    let bool
    if (event.currentTarget.dataset.bool == "list"){
      bool = true
    }else{
      bool = false
    }
    this.setData({
      list: bool
    })
  },

  reset: function () {
    let data = {}
    this.data.markers.forEach(marker => {
      data[marker.callout.content] = false
    })
    this.change_checked(data)
  },

  change_checked: function (data) {
    wx.setStorageSync('checked', data)
    this.setData({
      checked : data
    })
  },

  openBluetoothAdapter: function() {
    wx.openBluetoothAdapter({
      success: (res) => {
        this.startBeaconDiscovery()
        this.onBluetoothAdapterStateChange()
      },
      fail: (res) => {
        wx.showModal({
          title: "请打开蓝牙",
          showCancel: false,
          success: () => {
            this.openBluetoothAdapter()
          }
        })
      }
    })
  },

  onBluetoothAdapterStateChange: function() {
    wx.onBluetoothAdapterStateChange((res) => {
      if (res.available) {
        this.startBeaconDiscovery()
      } else {
        wx.showModal({
          title: "蓝牙不可用",
          showCancel: false,
          success: () => {
            this.openBluetoothAdapter()
          }
        })
      }
    })
  },

  startBeaconDiscovery: function() {
    wx.startBluetoothDevicesDiscovery({
      allowDuplicatesKey: true,
      success: (res) => {
        this.onBluetoothDeviceFound()
      },
    })
  },


  onBluetoothDeviceFound() {
    wx.onBluetoothDeviceFound((res) => {
      res.devices.forEach(device => {
        if (!device.name) {
          return
        } else if (!this.data.checked[device.name]){
          this.data.checked[device.name] = true
          this.change_checked(this.data.checked)
        }
      })
    })
  },
})